package com.loveprogrammer.handler;

import com.alibaba.fastjson2.JSON;
import com.loveprogrammer.base.network.support.BaseHandler;
import com.loveprogrammer.base.bean.session.Session;
import com.loveprogrammer.command.anotation.TagListener;
import com.loveprogrammer.command.anotation.TopicListener;
import com.loveprogrammer.base.network.support.SessionManager;
import com.loveprogrammer.command.client.ClientLoginTag;
import com.loveprogrammer.command.client.ClientTopic;
import com.loveprogrammer.command.server.ServerLoginOrRegisterTag;
import com.loveprogrammer.command.server.ServerTopic;
import com.loveprogrammer.constants.CommonValue;
import com.loveprogrammer.dto.base.StringRequest;
import com.loveprogrammer.pojo.StringMessage;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName HelloHandler
 * @Description 测试handler
 * @Author admin
 * @Date 2024/2/5 15:49
 * @Version 1.0
 */
@Component
@TopicListener(topic = ServerTopic.TOPIC_HELLO)
public class HelloHandler extends BaseHandler {

    public static final Logger log = LoggerFactory.getLogger(HelloHandler.class);

    @TagListener(tag = ServerLoginOrRegisterTag.TAG_HELLO_HI,messageClass = String.class)
    public StringMessage sayHi(ChannelHandlerContext ctx, String msg) {
        log.info("数据内容：data=" + msg);
        String result = "我是服务器，我收到了你的信息:" + msg;
        Session session = SessionManager.getInstance().getSessionByChannel(ctx.channel());
        result += ",sessionId = " + session.getId();


        StringMessage message = StringMessage.create(ClientTopic.TOPIC_LOGIN, ClientLoginTag.TAG_LOGIN_REGISTER);
        message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
        StringRequest request = new StringRequest(ClientTopic.TOPIC_LOGIN, ClientLoginTag.TAG_LOGIN_REGISTER);
        request.setContent(result);
        message.setBody(JSON.toJSONString(request));
        return message;
    }
}
